package com.ck.lock.lockLess;

import com.ck.lock.common.Counter;

/**
 * @author ck
 * @date 2023/7/3 16:17
 */
public class LockLessDemoTest {

    /**
     * 无锁的情况下
     * @param args
     * @throws InterruptedException
     */

    public static void main(String[] args) throws InterruptedException {
        System.out.println("目标结果是：100000");
        Counter counter=new Counter();

        //线程t1让count成员自增到5w
        Thread t1=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }

        });
        t1.start();

        //线程t2让count成员自增到5w
        Thread t2=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        t2.start();

        //等待两个线程都结束
        t1.join();
        t2.join();
        System.out.println("多线程处理下："+counter.getCount());
    }

    /**
     * 目标结果是：100000
     * 多线程处理下：58837
     *
     * 多线程情况下操作同一变量,数据可能会不正确
     */

}
